<?php

/**
 * @author Speedy2
 * @copyright 2012
 */

class InstallerCreator {
    private $name;
    private $version;    
    private $path;
    private $files;
    
    private $data;
    private $compressed_data;
    
    public function __construct($name, $version, $path, $files) {
        $this->name = $name;
        $this->version = $version;
        $this->path = $path;
        $this->files = $files;
        
        $this->data = array();
        $this->compressed_data = NULL;
    }
    
    public function createData() {
        $this->data = '<?php ';
        $this->data .= '$files = array(';
        
        foreach($this->files as $file) {
            $this->data .= 'array("filename" => "'.$file[0].'",'.
                '"update" => '.($file[1] ? 'true' : 'false').','.
                '"data" => "'.$this->compressFile($file[0]).'")';
        }
        
        $this->data .= ');';
    }
    
    public function compressFile($file) {
        $filename = $this->path.$file;
        //$filename = str_replace("/", "\\", $filename);
        $data = @file_get_contents($filename);       
        if (!$data) {
            return "";
        } 
        return base64_encode(gzdeflate($data));
    }
    
    public function exportPackage() {
        $fp = fopen($this->raw_name(), "wb");
        fwrite($fp, gzdeflate($this->data));
        fclose($fp);
    }
    
    private function raw_name() {
        $raw = str_replace(" ", "_", $this->name);
        $raw = strtolower($raw);
        $raw .= "_v".$this->version["major"];
        $raw .= ".".$this->version["minor"];
        $raw .= ".".$this->version["build"];
        $raw .= ".spp";
        
        return $raw;
    }
}

$name = "Speedy MVC";
$version = array("major" => 1, "minor" => 0, "build" => 0);

$files = array(
    array("index.php", true),
    array("controller/indexcontroller.php", false),
    array("controller/errorcontroller.php", false),
    array("include/autoload.class.php", true),
    array("include/client.class.php", NULL),
    array("include/config.php", false),
    array("include/controller.class.php", true),
    array("include/mainswitch.class.php", true),
    array("include/model.class.php", true),
    array("include/mysql.class.php", true),
    array("include/template.class.php", true),
    array("include/variableset.class.php", true),
    array("view/footer.php", false),
    array("view/header.php", false),
    array("view/index/index.php", false),
    array("view/error/error403.php", false),
    array("view/error/error404.php", false),
    array("view/error/error500.php", false),
    array("view/error/index.php", false)
);

$i = new InstallerCreator($name, $version, "../", $files);
$i->createData();
$i->exportPackage();

?>